import java.io.Console;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;

public class Menu {
	
	private static Console console = System.console();
	
	public static void main(String[] args) throws Exception, ClassNotFoundException,SQLException,NoSuchAlgorithmException,UnsupportedEncodingException,IOException{
		
		while (UserLogin.userID == 0) {
			intro();
		}
		System.out.println("\n------------------------------------");
		System.out.println("\nWelcome!");
		
		while (UserLogin.userID != 0) {
			menu();
		}
		
	}
	
	public static void intro() throws ClassNotFoundException,SQLException,NoSuchAlgorithmException,UnsupportedEncodingException,IOException{
		
		String option = "0";
		
		// Welcome
		System.out.println("\n\n------------------------------------");
		System.out.println("Welcome! Please login or register.");
		System.out.println("------------------------------------");

		
		while (!(option.equals("1") || option.equals("2"))) {
			
			// Login or Register
			System.out.println("\nPlease choose an option (enter a number):");
			System.out.println("1. Login");
			System.out.println("2. Register");
			
			option = console.readLine("> ");
		}
		
		if (option.equals("1")) {
			UserLogin.login();
		}
		else if (option.equals("2")) {
			UserRegistration.register();
		}
		// This shouldn't happen
		else {
			System.out.println("error");
		}
		
	}

	public static void menu() throws Exception,SQLException,ClassNotFoundException{
		String option = "0";

		while(!(option.equals("1") || option.equals("2") || option.equals("3") || option.equals("4") || option.equals("5") || option.equals("6") || option.equals("7") || option.equals("8"))) {
			System.out.println("\nPlease choose an option (enter a number):");
			System.out.println("1. Upload Item");
			System.out.println("2. Browse Items By Category");
			System.out.println("3. Get More Info On Item");
			System.out.println("4. Create Offer");
			System.out.println("5. Check For Offers");
			System.out.println("6. Accept Offers/Trade");
			System.out.println("7. Decline Offers");
			System.out.println("8. Logout");

			option = console.readLine("> ");
		}
		

		if (option.equals("1")) {
			Upload.upload();
		}
		
		else if (option.equals("2")) {
			ByCategory.byCat();
		}
			
		// 3. Get More Info On Item

		else if (option.equals("3")) {
			MoreInfo.moreInfo();
		}
		// 4. Create Offer
		else if (option.equals("4")) {
			CreateOffer.createOffer();
		}
		// 5. Check For Offers
		else if (option.equals("5")) {
			OfferActions.viewOffers();
		}
		// 6. Accept Offers/Trade
		else if (option.equals("6")) {
			OfferActions.acceptOffer();
		}
		else if (option.equals("7")) {
			OfferActions.declineOffer();
		}
		else if (option.equals("8")) {
			String input = "";
			System.out.println("\nAre you sure you want to logout? (y/n)");
			while (!(input.equals("n") || input.equals("n"))) {
				input = console.readLine("> ");
				if (input.equals("y")) {
					System.out.println("\nGoodbye!\n");
					UserLogin.userID = 0;
					System.exit(0);
				}
				else if (input.equals("n")) {
					break;
				}
				else {
					System.out.println("Please choose (y/n)");
				}
			}
		}
	}
}
